//
//  DataBaseManager.m
//  Fragrance
//
//  Created by dllo on 15/5/29.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "DataBaseManager.h"

static sqlite3 *dbPoint = nil;

@implementation DataBaseManager

+ (void)openDataBase
{
    if (dbPoint) {
        return;
    }
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    path = [path stringByAppendingPathComponent:@"food.rdb"];
    sqlite3_open(path.UTF8String, &dbPoint);
}

+ (void)createTable
{
    [self openDataBase];
    NSString *sql = [NSString stringWithFormat:@"create table foods(name varchar(20), desc varchar(100) ,favoriteNum varchar(10), shopName varchar(20))"];
    sqlite3_exec(dbPoint, sql.UTF8String, NULL, NULL, NULL);
}
+ (void)insertValue:(CftModelData *)model
{
    [self openDataBase];
    NSString *sql = [NSString stringWithFormat:@"insert into foods values('%@', '%@', '%@', '%@')", model.name, model.desc, model.favoriteNum,  model.shopName];
    sqlite3_exec(dbPoint, sql.UTF8String, NULL, NULL, NULL);
    
}

+ (BOOL)deleteValue:(NSString *)name
{
    [self openDataBase];
    
    NSString *sql = [NSString stringWithFormat:@"delete from foods where name= '%@'",name];
    
    BOOL reuslut =  sqlite3_exec(dbPoint, sql.UTF8String, NULL, NULL, NULL);
    if (reuslut == SQLITE_OK) {
        return YES;
    }
    else
    {
        return NO;
    }
}
+ (void)deleteValue
{
    [self openDataBase];
    NSString *sql = [NSString stringWithFormat:@"delete from foods"];
    sqlite3_exec(dbPoint, sql.UTF8String, NULL, NULL, NULL);
}

+ (void)updateValue
{

}

+ (NSMutableArray *)selectData
{
    [self openDataBase];
    sqlite3_stmt *stmt = nil;
    NSString *sql = [NSString stringWithFormat:@"select * from foods"];
    
    int result = sqlite3_prepare_v2(dbPoint, sql.UTF8String, -1, &stmt, NULL);
    if (result == SQLITE_OK) {
        NSMutableArray *array = [NSMutableArray array];
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *name = sqlite3_column_text(stmt, 0);
            const unsigned char *desc = sqlite3_column_text(stmt, 1);
            const unsigned char *favoriteNum = sqlite3_column_text(stmt, 2);
            const unsigned char *shopName = sqlite3_column_text(stmt, 3);
            //封装成model
            CftModelData *model = [[CftModelData alloc] init];
            model.name = [NSString stringWithUTF8String:(const char *)name];
            model.desc = [NSString stringWithUTF8String:(const char *)desc];
            
            model.favoriteNum = [NSString stringWithUTF8String:(const char *)favoriteNum];
            model.shopName =  [NSString stringWithUTF8String:(const char *)shopName];
            [array addObject:model];
            

        }
        sqlite3_finalize(stmt);
        return array;
    }
    sqlite3_finalize(stmt);
    return [NSMutableArray array];
    
}
+ (BOOL)isExists:(NSString *)foodName
{
    NSMutableArray *array = [DataBaseManager selectData];
    for (CftModelData *model1 in array) {
        
        if ([foodName isEqualToString:model1.name]) {
        
            return YES;
        }
        
    }
    return NO;
}

@end
